/* Printer status information */
/** @file

    Handles cartridge information */
/* Created 22.03.2005 T. Milius
   Changed 22.03.2005 T. Milius */
/* (c) Copyright 2005 by Thomas Milius Stade, Germany
   Source must not be altered without agreement of the owner.
   The owner of the source is allowed to use this code inside programs without
   publishing the code of this programs. These programs may be commercial.
   Other developers can use this source freely inside own software if the source
   code of this programs is made public to same conditions like valid to this code
   and no commercial profit is taken from the programs based on this code

   Code or parts of it are not allowed to be used within GPL code or
   similar licenses which are "infecting" other code and trying to "supersede"
   other licenses. */
/* ANSI C */

#ifndef cartrinfo_h
#define cartrinfo_h

/* !!!!!!!!!! libraries !!!!!!!!!! */
/* ---------- ANSI-C ---------- */
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* ------------ own ------------ */
#include "variables.h"

/* !!!!!!!!!!! definitions !!!!!!!!!! */

/* !!!!!!!!!! data structures !!!!!!!!!! */

/* !!!!!!!!!! support functions !!!!!!!!!! */

/* !!!!!!!!!! functions !!!!!!!!!! */
/** Searches the printer Info Cartridge database file
    for the cartridges of a given cartridge manufacturer
    belonging to the actual printer which is internally
    determined from the given general variables.

    The result is stored also inside the given variable block
    in certain variables CTRTYP_ and RMRK_ . This variables
    can be used inside DRAW files to show name and information
    of a cartridge. See <A HREF="../../../Help/1/Index.html#variables">variables</A>
    in the manual for more details. */
bool set_cartridge_info(struct variable_struct *general_variables,
                       char *required_cartridge_manufacturer)
{
char input_line[MAX_STRING_LENGTH];
char cartridge_manufacturer[MAX_MESSAGE_LENGTH];
char manufacturer[MAX_MESSAGE_LENGTH];
char printer_type[MAX_MESSAGE_LENGTH];
char cartridge_type[MAX_CARTRIDGE_TYPE_LENGTH];
char remark[MAX_REMARK_LENGTH];
int actual_territory;
int actual_cartridge;
int territory;
char *actual_variable;
char variable_name[15];
FILE *cartridge_information_file;

if (!general_variables) return false;
drop_cartridge_variables(general_variables);
strcpy(general_variables->cartridge_manufacturer, required_cartridge_manufacturer);
territory=atoi(getenv("Territory"));
if ((cartridge_information_file=fopen("<PrtInfo$Dir>.Resources.CtrInfo", "r")) == NULL) {
  return false;
  }
while(!feof(cartridge_information_file)) {
  if (get_line(cartridge_information_file,
               input_line)) {
    strcpy(cartridge_manufacturer, "");
    strcpy(manufacturer, "");
    strcpy(printer_type, "");
    strcpy(cartridge_type, "");
    strcpy(remark, "");
    actual_territory=-1;
    actual_cartridge=-1;
    sscanf(input_line,
           "%[^,],%[^,],%x,%[^,],%d,%[^,],%[^,]",
           manufacturer,
           printer_type,
           &actual_cartridge,
           cartridge_manufacturer,
           &actual_territory,
           cartridge_type,
           remark);
    if ((strcmp(general_variables->manufacturer, manufacturer) == 0) &&
        (strcmp(general_variables->printer_type, printer_type) == 0) &&
        (strcmp(required_cartridge_manufacturer, cartridge_manufacturer) == 0) &&
        (actual_cartridge >= MIN_COLOUR) &&
        (actual_cartridge <= MAX_CARTRIDGE)) {
      if ((actual_territory == territory) ||
          (actual_territory == 0)) {
        if (strlen(cartridge_type) > 0) {
          sprintf(variable_name,
                  "CTRTYP_%X",
                  actual_cartridge);
          actual_variable=(char *)get_address_of_variable(general_variables,
                                                          variable_name,
                                                          NULL,
                                                          NULL);
          if (actual_variable) {
            strcpy(actual_variable, cartridge_type);
            }
          }
        if (strlen(remark) > 0) {
          sprintf(variable_name,
                  "RMRK_%X",
                  actual_cartridge);
          actual_variable=(char *)get_address_of_variable(general_variables,
                                                          variable_name,
                                                          NULL,
                                                          NULL);
          if (actual_variable) {
            strcpy(actual_variable, remark);
            }
          }
        }
      }
    }
  }
fclose(cartridge_information_file);
return true;
}

#endif
